// pages/apply/apply.js
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');

Page({
  data: {
    // tagList: [{ tag: "我爱瘦身" }, { tag: "甩肉行动" }, { tag: "S身材的诱惑" }, { tag: "明明白白瘦身" }, { tag: "燃脂" }]
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.data.id = options.id;
    this.data.title = options.title;
    console.log(" options-" + this.data.id + " - " + this.data.title);
    this.fetchData(options.id);
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  fetchData: function (id) {
    console.log(id);
    var self = this;
    self.setData({
      hidden: false
    });

    wx.request({
      url: Api.getTrialTags(id, { mdrender: false }),
      header: Api.headers,
      success: function (res) {
        console.log(JSON.stringify(res));
        self.setData({
          tagList: res.data.data
        });
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
      }
    });
  },
  bindTextAreaBlur: function (e) {
    console.log(e.detail.value);
    this.data.reason = e.detail.value;
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    this.data.tags = e.detail.value;
  },
  // 提交注册信息
  submit: function (obj) {
    var self = this;
    console.log('submit - ');
    if (this.data.sended) {
      return;
    }

    self.data.sended = true;
    wx.showNavigationBarLoading();
    var tryId = self.data.id;
    var userId = util.getUserId();
    var title = self.data.title;
    var reason = self.data.reason;
    var tags = self.data.tags;
    if (reason === '') {
      wx.showToast({
        title: '请填写申请理由',
        icon: 'loading',
        duration: 500
      })
      return;
    }
    if (tags === '') {
      wx.showToast({
        title: '请选择产品标签',
        icon: 'loading',
        duration: 500
      })
      return;
    }
    var value = {
      tryout_id: tryId,
      uid: userId,
      title: title,
      reason: reason,
      tags: tags
    };
    console.log("apply trial submit" + JSON.stringify(value));
    console.log("Api.tryApply" + Api.tryApply);
    wx.request({
      url: Api.tryApply,
      method: 'POST',
      data: value,
      header: Api.headers,
      success: function (res) {
        wx.hideNavigationBarLoading();
        console.log("apply submit" + JSON.stringify(res));
        var data = res.data;

        if (data.code != 1) {
          wx.showModal({
            title: '提示',
            content: data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          self.data.sended = false;
          return;
        }

        wx.showToast({
          title: data.msg,
          duration: 500
        })

        wx.navigateBack();

      },
      fail: function (res) {
        wx.hideNavigationBarLoading();
        self.data.sended = false;
        wx.showToast({
          title: '服务器异常<br>请稍后重试',
          icon: 'loading',
          duration: 500
        })
      },
    });
  }
})